package com.aacar.op.service.user;

import com.aacar.op.dao.user.UserDao;
import com.aacar.op.entity.user.User;
import com.aacar.op.utils.web.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Service(value = "userService")
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDao userDao;

    //强制初始化.
    @PostConstruct
    public void init() {

    }

    @Override
    public User get(Long id) {
        return userDao.findOne(id);
    }

    @Override
    public int delete(Long id) {
        return userDao.delete(id);
    }

    @Override
    public int saveOrUpdate(User entity) {
        int ret;
        entity.auditObject();
        if (entity.isNew()) {
            ret = userDao.insert(entity);
        } else {
            ret = userDao.update(entity);
        }
        return ret;
    }

    @Override
    public PageInfo<User> getPage(Map<String, Object> params, Pageable pageable) {
        PageUtils.mergeSort(params, pageable);
        //pageNum(页码)从1开始,spring data common domain从0开始的.
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        List<User> pageContent = userDao.findAll(params);
        return new PageInfo(pageContent);
    }

    @Override
    public User getByLoginName(String loginName) {
        return userDao.findByLoginName(loginName);
    }

    @Override
    public boolean validateLoginName(Long id, String loginName) {
        User entity = userDao.findByLoginName(loginName);
        boolean isValid;
        if (entity == null) {  //loginName
            isValid = true;
        } else if (id == null) {   //新增操作
            isValid = (entity == null);
        } else {   //修改操作
            isValid = (entity.getId().equals(id));
        }
        return isValid;
    }

    @Override
    public boolean validateMobile(Long id, String mobile) {
        User entity = userDao.findByMobile(mobile);
        boolean isValid;
        if (entity == null) {  //loginName
            isValid = true;
        } else if (id == null) {   //新增操作
            isValid = (entity == null);
        } else {   //修改操作
            isValid = (entity.getId().equals(id));
        }
        return isValid;
    }

    @Override
    public boolean validateEmail(Long id, String email) {
        User entity = userDao.findByEmail(email);
        boolean isValid;
        if (entity == null) {  //loginName
            isValid = true;
        } else if (id == null) {   //新增操作
            isValid = (entity == null);
        } else {   //修改操作
            isValid = (entity.getId().equals(id));
        }
        return isValid;
    }

    @Override
    public User updateUser(User user) {
        if (!validateLoginName(user.getId(), user.getLoginName())) {
            throw new ServiceException("账号已存在:" + user.getLoginName());
        }
        //密文密码不为空是保存.
        if (StringUtils.isNotBlank(user.getPlainPassword())) {
            entryptPassword(user);
        }
        saveOrUpdate(user);
        return user;
    }

    @Override
    public User registerUser(User user) {

        if (!validateLoginName(user.getId(), user.getLoginName())) {
            throw new ServiceException("账号已存在:" + user.getLoginName());
        }
        //密码加密处理保存
        entryptPassword(user);
        user.auditObject();
        saveOrUpdate(user);
        return user;
    }

    //-------------用户密码加密------------//

    /**
     * 设定安全的密码，MD5简单加密.
     */
    private void entryptPassword(User user) {
        String password = new SimpleHash("MD5", user.getPlainPassword()).toHex();
        user.setPassword(password);
    }
}

